﻿using Application.AppSrevices;
using Application.Dto;
using Application.IAppSrevices;
using AutoMapper;
using Domain.Entitys;
using Domain.Repositorys;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using UI.Hubs;

namespace UI.Controllers
{
    public class HFController : Controller
    {
        private readonly IWebHostEnvironment webHostEnvironment;
        private readonly ICFFKService cFFKService;
        private readonly ICFTKRepository cFTKRepository;
        private readonly ICFSKRepositoty cFSKRepositoty;
        private readonly ICMKService cMKService;
        private readonly ICMService cMService;
        private readonly ICPCService cPCService;
        private readonly ISSService sSService;
        private readonly IMapper mapper;
        private readonly IHFService hFService;
        private readonly ICFSKService cFSKService;
        private readonly ICFTKService cFTKService;
        private readonly IERService eRService;

        public HFController(IWebHostEnvironment webHostEnvironment, ICFFKService cFFKService, ICFTKRepository cFTKRepository, ICFSKRepositoty cFSKRepositoty, ICMKService cMKService, ICMService cMService, ICPCService cPCService,
            ISSService sSService, IMapper mapper, IHFService hFService, ICFSKService cFSKService, ICFTKService cFTKService, IERService eRService)
        {
            this.webHostEnvironment = webHostEnvironment;
            this.cFFKService = cFFKService;
            this.cFTKRepository = cFTKRepository;
            this.cFSKRepositoty = cFSKRepositoty;
            this.cMKService = cMKService;
            this.cMService = cMService;
            this.cPCService = cPCService;
            this.sSService = sSService;
            this.mapper = mapper;
            this.hFService = hFService;
            this.cFSKService = cFSKService;
            this.cFTKService = cFTKService;
            this.eRService = eRService;
        }

        /// <summary>
        /// 人力资源档案登记页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(int id)
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            TempData["id"] = id;
            return View();
        }
        public IActionResult Index1()
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            return View();
        }
        //查询ER录用通过的数据
        public async Task<IActionResult> SelectId()
        {
            int id = (int)TempData["id"];
            ER er = await eRService.SelectId(id);
            return Json(er);
        }

        #region 查询下拉框数据
        /// <summary>
        /// 查询职称数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ZCSelect()
        {
            return Json(await cPCService.CPCFillAsync("职称"));
        }
        /// <summary>
        /// 查询国籍数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GJSelect()
        {
            return Json(await cPCService.CPCFillAsync("国籍"));
        }
        /// <summary>
        /// 查询民族数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> MZSelect()
        {
            return Json(await cPCService.CPCFillAsync("民族"));
        }
        /// <summary>
        /// 查询宗教信仰数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ZJXYSelect()
        {
            return Json(await cPCService.CPCFillAsync("宗教信仰"));
        }
        /// <summary>
        /// 查询政治面貌数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ZZMMSelect()
        {
            return Json(await cPCService.CPCFillAsync("政治面貌"));
        }
        /// <summary>
        /// 查询学历数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> XLSelect()
        {
            return Json(await cPCService.CPCFillAsync("学历"));
        }
        /// <summary>
        /// 查询薪酬标准数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> XCBZSelect()
        {
            return Json(await sSService.SSFill());
        }
        /// <summary>
        /// 查询教育年限数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> JYNXSelect()
        {
            return Json(await cPCService.CPCFillAsync("教育年限"));
        }
        /// <summary>
        /// 查询学历专业数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> XLZYSelect()
        {
            return Json(await cPCService.CPCFillAsync("学历专业"));
        }
        /// <summary>
        /// 查询特长数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> TCSelect()
        {
            return Json(await cPCService.CPCFillAsync("特长"));
        }
        /// <summary>
        /// 查询爱好数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> AHSelect()
        {
            return Json(await cPCService.CPCFillAsync("爱好"));
        }
        /// <summary>
        /// 查询职位数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("HF/CMFillAsync")]
        public async Task<IActionResult> CMFillAsync()
        {
            return Json(await cMService.CMFillAsync());
        }
        #endregion

        [HttpPost("HF/Upload")]
        public IActionResult Upload([FromForm] IFormFile image)
        {
            if (image != null && image.Length > 0)
            {
                // 生成新的图片名称，格式为日期加时间并追加三位随机数
                var now = DateTime.Now;
                var random = new Random();
                var randomDigits = random.Next(100, 1000).ToString();
                var newFileName = $"{now:yyyyMMddHHmmss}{randomDigits}{Path.GetExtension(image.FileName)}";

                // 假设图片存储在wwwroot/uploads目录下
                var uploadsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads");
                if (!Directory.Exists(uploadsPath))
                {
                    Directory.CreateDirectory(uploadsPath);
                }
                var filePath = Path.Combine(uploadsPath, newFileName);
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    image.CopyTo(stream);
                }
                // 返回图片的完整地址
                var baseUrl = $"{Request.Scheme}://{Request.Host.Value}";
                var relativePath = Path.Combine("uploads", newFileName);
                var fullUrl = Path.Combine(baseUrl, relativePath).Replace("\\", "/");
                return Json(fullUrl);
            }
            return BadRequest("No image file provided");
        }

        /// <summary>
        /// 添加人力资源档案信息
        /// </summary>
        /// <param name="hF"></param>
        /// <returns></returns>
        [TypeFilter(typeof(SingarFilterAttribute), Arguments = new object[] { "人力资源档案管理" })]
        [HttpPost("HF/HFAddAsync")]
        public async Task<IActionResult> HFAddAsync(HFAddDto hF)
        {
            int num = await hFService.HFAddAsync(hF);
            return Json(num);
        }

        /// <summary>
        /// 人力资源档案复核页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFReview()
        {
            return View();
        }

        /// <summary>
        /// 分页查询人力资源档案信息
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet("HF/HFFenYeAsync")]
        public async Task<IActionResult> HFFenYeAsync(int pageSize, int pageIndex)
        {
            return Json(await hFService.HFFenYeAsync(pageSize, pageIndex));
        }

        /// <summary>
        /// 查询人力资源总数
        /// </summary>
        /// <returns></returns>
        [HttpGet("HF/HFCountAsybc")]
        public async Task<IActionResult> HFCountAsybc()
        {
            return Json(await hFService.HFCountAsync());
        }

        /// <summary>
        /// 人力资源档案复核页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFReviewInfo(int id)
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            TempData["id"] = id;
            return View();
        }

        /// <summary>
        /// 根据id查询对应的人力资源信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("HF/HFByIDAsync")]
        public async Task<IActionResult> HFByIDAsync()
        {
            int id = (int)TempData["id"];
            return Json(await hFService.HFByIDAsync(id));
        }

        /// <summary>
        /// 进行人力资源档案复核操作
        /// </summary>
        /// <param name="hF"></param>
        /// <returns></returns>
        [HttpPut("HF/HFReviewAsync")]
        public async Task<IActionResult> HFReviewAsync(HFUpdateDto hF)
        {
            int num = await hFService.HFReviewAsync(hF);
            return Json(num);
        }

        /// <summary>
        /// 人力资源档案查询页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFSelect()
        {
            return View();
        }

        /// <summary>
        /// 显示机构级联下拉框数据
        /// </summary>
        /// <returns></returns>
        public IActionResult Cascaders2()
        {
            return Json(cFTKService.Cascaders2());
        }

        /// <summary>
        /// 显示职位级联下拉框数据
        /// </summary>
        /// <returns></returns>
        public IActionResult Cascaders()
        {
            return Json(cMService.Cascaders());
        }

        /// <summary>
        /// 多条件查询人力资源数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="indexPage"></param>
        /// <param name="fid"></param>
        /// <param name="sid"></param>
        /// <param name="tid"></param>
        /// <param name="mkid"></param>
        /// <param name="mid"></param>
        /// <param name="keywords"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet("HF/HFWhereList")]
        public IActionResult HFWhereList(int pageSize, int indexPage, string fid, string sid, string tid, string mkid, string mid, string keywords, string startDate, string endDate)
        {
            FenYe<HF> hf = new FenYe<HF>()
            {
                CurrentPage = indexPage,
                PageSize = pageSize
            };
            hFService.HFWhereList(hf, fid, sid, tid, mkid, mid, keywords, startDate, endDate);
            return Json(hf);
        }

        /// <summary>
        /// 人力资源档案管理查看详细页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFSelectInfo(int id)
        {
            TempData["id"] = id;
            return View();
        }

        /// <summary>
        /// 人力资源管理档案变更页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFChange()
        {
            return View();
        }

        /// <summary>
        /// 人力资源管理档案变更操作页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFChangeInfo(int id)
        {
            string userName = HttpContext.Session.GetString("UTrueName");
            ViewBag.UserTrueName = userName;
            TempData["id"] = id;
            return View();
        }

        /// <summary>
        /// 多条件查询人力资等待复核数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="indexPage"></param>
        /// <param name="fid"></param>
        /// <param name="sid"></param>
        /// <param name="tid"></param>
        /// <param name="mkid"></param>
        /// <param name="mid"></param>
        /// <param name="keywords"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet("HF/HFWhereList2")]
        public IActionResult HFWhereList2(int pageSize, int indexPage, string fid, string sid, string tid, string mkid, string mid, string keywords, string startDate, string endDate)
        {
            FenYe<HF> hf = new FenYe<HF>()
            {
                CurrentPage = indexPage,
                PageSize = pageSize
            };
            hFService.HFWhereList2(hf, fid, sid, tid, mkid, mid, keywords, startDate, endDate);
            return Json(hf);
        }

        /// <summary>
        /// 人力资源档案变更操作
        /// </summary>
        /// <param name="hFChangeUpdateDto"></param>
        /// <returns></returns>
        [HttpPut("HF/HFChangeUpdateAsync")]
        public async Task<IActionResult> HFChangeUpdateAsync(HFChangeUpdateDto hFChangeUpdateDto)
        {
            int num = await hFService.HFChangeUpdateAsync(hFChangeUpdateDto);
            return Json(num);
        }

        /// <summary>
        /// 人力资源档案删除页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFDelete()
        {
            return View();
        }

        /// <summary>
        /// 多条件查询人力资未复核，正常状态数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="indexPage"></param>
        /// <param name="fid"></param>
        /// <param name="sid"></param>
        /// <param name="tid"></param>
        /// <param name="mkid"></param>
        /// <param name="mid"></param>
        /// <param name="keywords"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet("HF/HFWhereList3")]
        public IActionResult HFWhereList3(int pageSize, int indexPage, string fid, string sid, string tid, string mkid, string mid, string keywords, string startDate, string endDate)
        {
            FenYe<HF> hf = new FenYe<HF>()
            {
                CurrentPage = indexPage,
                PageSize = pageSize
            };
            hFService.HFWhereList3(hf, fid, sid, tid, mkid, mid, keywords, startDate, endDate);
            return Json(hf);
        }

        /// <summary>
        /// 人力资源档案删除详细页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult HFDeleteInfo(int id)
        {
            TempData["id"] = id;
            return View();
        }

        /// <summary>
        /// 人力资源档案删除
        /// </summary>
        /// <param name="hFDeleteDto"></param>
        /// <returns></returns>
        [HttpPut("HF/HFDeleteAsync")]
        public async Task<IActionResult> HFDeleteAsync(HFDeleteDto hFDeleteDto)
        {
            int num = await hFService.HFDeleteAsync(hFDeleteDto);
            return Json(num);
        }

        /// <summary>
        /// 人力资源档案恢复页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFRestore()
        {
            return View();
        }

        /// <summary>
        /// 多条件查询人力资档案已删除数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="indexPage"></param>
        /// <param name="fid"></param>
        /// <param name="sid"></param>
        /// <param name="tid"></param>
        /// <param name="mkid"></param>
        /// <param name="mid"></param>
        /// <param name="keywords"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        [HttpGet("HF/HFWhereList4")]
        public IActionResult HFWhereList4(int pageSize, int indexPage, string fid, string sid, string tid, string mkid, string mid, string keywords, string startDate, string endDate)
        {
            FenYe<HF> hf = new FenYe<HF>()
            {
                CurrentPage = indexPage,
                PageSize = pageSize
            };
            hFService.HFWhereList4(hf, fid, sid, tid, mkid, mid, keywords, startDate, endDate);
            return Json(hf);
        }

        /// <summary>
        /// 人力资源档案恢复详细页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFRestoreInfo(int id)
        {
            TempData["id"] = id;
            return View();
        }

        /// <summary>
        /// 人力资源档案恢复
        /// </summary>
        /// <param name="hFRestoreDto"></param>
        /// <returns></returns>
        [HttpPut("HF/HFHFRestoreAsync")]
        public async Task<IActionResult> HFHFRestoreAsync(HFRestoreDto hFRestoreDto)
        {
            int num = await hFService.HFHFRestoreAsync(hFRestoreDto);
            return Json(num);
        }

        /// <summary>
        /// 人力资源档案永久删除页面
        /// </summary>
        /// <returns></returns>
        public IActionResult HFPermanentlyDelete()
        {
            return View();
        }

        /// <summary>
        /// 查询人力资源档案已删除数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("HF/HFDeleteInfoAsync")]
        public async Task<IActionResult> HFDeleteInfoAsync()
        {
            return Json(await hFService.HFDeleteInfoAsync());
        }
        /// <summary>
        /// 查询人力资源档案已删除数量
        /// </summary>
        /// <returns></returns>
        [HttpGet("HF/HFDeleteInfoCount")]
        public async Task<IActionResult> HFDeleteInfoCount()
        {
            return Json(await hFService.HFDeleteInfoCount());
        }

        /// <summary>
        /// 永久删除人力资源档案数据
        /// </summary>
        /// <param name="hF"></param>
        /// <returns></returns>
        [HttpDelete("HF/HFDelete1")]
        public async Task<IActionResult> HFDelete1(HFDelete1 hF)
        {
            return Json(await hFService.HFDelete(hF));
        }
    }
}
